/**
 * 
 */
package jmine.tec.report.metadata.impl;

import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;

import jmine.tec.report.cell.DataCell;
import jmine.tec.report.cell.impl.StringDataCell;
import jmine.tec.report.chart.ChartType;
import jmine.tec.report.metadata.ColumnMetaData;
import jmine.tec.report.metadata.ParameterDefinition;
import jmine.tec.report.metadata.ParameterType;

/**
 * Unmodifiable implementation of the column metadata.
 * 
 * @author takeshi
 * @version 21/08/2007
 */
public class UnmodifiableColumnMetaData implements ColumnMetaData {

    private static final long serialVersionUID = 5799975948884957962L;

    private static final DataCell EMPTY_DATA_CELL = new StringDataCell(" ");

    private final String key;

    private final String label;

    private final Map<String, ParameterDefinition> parameters;

    private final DataCell exampleDataCell;

    private final EnumSet<ChartType> chartTypes;

    /**
     * Copy c'tor
     * 
     * @param meta {@link ColumnMetaData}
     * @param defs {@link ParameterDefinition} a mais
     */
    public UnmodifiableColumnMetaData(final ColumnMetaData meta, final ParameterDefinition... defs) {
        this.key = meta.getKey();
        this.label = meta.getLabel();
        final Map<String, ParameterDefinition> def = mergeMetaData(meta, defs);
        this.parameters = Collections.unmodifiableMap(def);
        this.exampleDataCell = meta.getExampleDataCell();
        this.chartTypes = EnumSet.copyOf(meta.getChartTypes());
    }

    /**
     * C'tor
     * 
     * @param key the column key
     * @param label the column label
     * @param exampleDataCell the example data cell
     * @param chartTypes the chart types
     * @param params the params to add
     */
    public UnmodifiableColumnMetaData(final String key, final String label, final DataCell exampleDataCell,
            final EnumSet<ChartType> chartTypes, final ParameterDefinition... params) {
        this(key, label, mergeParameters(null, params), exampleDataCell, EnumSet.copyOf(chartTypes));
    }

    /**
     * C'tor
     * 
     * @param key the column key
     * @param name the column name
     * @param values the parameter definitions
     * @param params overriding parameters
     */
    public UnmodifiableColumnMetaData(final String key, final String name,
            final Collection<ParameterDefinition> values, final ParameterDefinition... params) {
        this(key, name, mergeParameters(values, params), EMPTY_DATA_CELL, EnumSet.noneOf(ChartType.class));
    }

    /**
     * Merge parameters from the collection
     * 
     * @param values {@link Collection} of {@link ParameterDefinition}
     * @param params {@link ParameterDefinition}
     * @return {@link Map}
     */
    private static Map<String, ParameterDefinition> mergeParameters(final Collection<ParameterDefinition> values,
            final ParameterDefinition... params) {
        final Map<String, ParameterDefinition> def = new HashMap<String, ParameterDefinition>();
        if (values != null) {
            for (final ParameterDefinition parameterDefinition : values) {
                def.put(parameterDefinition.getName(), parameterDefinition);
            }
        }
        for (final ParameterDefinition parameterDefinition : params) {
            def.put(parameterDefinition.getName(), parameterDefinition);
        }
        return def;
    }

    /**
     * Private C'tor
     * 
     * @param key the key
     * @param label the label
     * @param parameters the parameters
     * @param exampleDataCell the example data cell
     * @param chartTypes the chart types
     */
    private UnmodifiableColumnMetaData(final String key, final String label,
            final Map<String, ParameterDefinition> parameters, final DataCell exampleDataCell,
            final EnumSet<ChartType> chartTypes) {
        super();
        this.key = key;
        this.label = label;
        this.parameters = parameters;
        this.exampleDataCell = exampleDataCell;
        this.chartTypes = chartTypes;
    }

    /**
     * C'tor that overrides key and name
     * 
     * @param key the key
     * @param name the name
     * @param meta the base metadata
     * @param defs the overriding parameter definitions
     */
    public UnmodifiableColumnMetaData(final String key, final String name, final ColumnMetaData meta,
            final ParameterDefinition... defs) {
        this(key, name, mergeMetaData(meta, defs), meta.getExampleDataCell(), EnumSet.copyOf(meta.getChartTypes()));
    }

    /**
     * Merges a {@link ColumnMetaData} parameters with the given
     * 
     * @param meta {@link ColumnMetaData}
     * @param defs the {@link ParameterDefinition} to merge
     * @return {@link Map}
     */
    private static Map<String, ParameterDefinition> mergeMetaData(final ColumnMetaData meta,
            final ParameterDefinition... defs) {
        final Map<String, ParameterDefinition> def = new HashMap<String, ParameterDefinition>();
        def.putAll(meta.getAllParameterDefinitions());
        for (final ParameterDefinition parameterDefinition : defs) {
            def.put(parameterDefinition.getName(), parameterDefinition);
        }
        return def;
    }

    /**
     * C'tor
     * 
     * @param key String
     * @param name String
     * @param defs the parameter definitions
     */
    public UnmodifiableColumnMetaData(final String key, final String name, final ParameterDefinition... defs) {
        this(key, name, mergeParameters(null, defs), EMPTY_DATA_CELL, EnumSet.noneOf(ChartType.class));
    }

    /**
     * @param key2 the key
     * @param entitiesMap the entities map
     * @see br.com.maps.util.report.metadata.ColumnMetaData#addEntityParameter(java.lang.String, java.util.Map)
     * @return {@link ColumnMetaData}
     */
    public ColumnMetaData addEntityParameter(final String key2, final Map<String, String> entitiesMap) {
        return new UnmodifiableColumnMetaData(this, new ParameterDefinition(key2, key2, entitiesMap,
                ParameterType.ENTITY));
    }

    /**
     * @param parameterDefinition the parameter to add
     * @return {@link ColumnMetaData}
     */
    public ColumnMetaData addParameter(final ParameterDefinition parameterDefinition) {
        return new UnmodifiableColumnMetaData(this, parameterDefinition);
    }

    /**
     * Adds a parameter returning a new {@link ColumnMetaData}
     * 
     * @param name String
     * @param defaultValue String
     * @param type the type
     * @return {@link ColumnMetaData}
     */
    public ColumnMetaData addParameter(final String name, final Object defaultValue, final ParameterType type) {
        return new UnmodifiableColumnMetaData(this, new ParameterDefinition(name, name, defaultValue, type));
    }

    /**
     * Adds a parameter returning a new {@link ColumnMetaData}
     * 
     * @param name String
     * @param lab String
     * @param referenceValue the ref value
     * @param type the type
     * @return {@link ColumnMetaData}
     */

    public ColumnMetaData addParameter(final String name, final String lab, final Object referenceValue,
            final ParameterType type) {
        return new UnmodifiableColumnMetaData(this, new ParameterDefinition(name, lab, referenceValue, type));
    }

    /**
     * @param obj the other
     * @return boolean
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (this.getClass() != obj.getClass()) {
            return false;
        }
        final UnmodifiableColumnMetaData other = (UnmodifiableColumnMetaData) obj;
        if (this.key == null) {
            if (other.key != null) {
                return false;
            }
        } else if (!this.key.equals(other.key)) {
            return false;
        }
        if (this.label == null) {
            if (other.label != null) {
                return false;
            }
        } else if (!this.label.equals(other.label)) {
            return false;
        }
        if (this.parameters == null) {
            if (other.parameters != null) {
                return false;
            }
        } else if (!this.parameters.equals(other.parameters)) {
            return false;
        }
        return true;
    }

    /**
     * @return {@link Map}
     * @see br.com.maps.util.report.metadata.ColumnMetaData#getAllParameterDefinitions()
     */
    public Map<String, ParameterDefinition> getAllParameterDefinitions() {
        return this.parameters;
    }

    /**
     * @return a copy of this columns parameter
     * @see br.com.maps.util.report.metadata.ColumnMetaData#getAllParameters()
     */
    public Map<String, ParameterDefinition> getAllParameters() {
        return new HashMap<String, ParameterDefinition>(this.parameters);
    }

    /**
     * @return String
     */
    public String getKey() {
        return this.key;
    }

    /**
     * @return String
     */
    public String getLabel() {
        return this.label;
    }

    /**
     * @param localkey {@link String}
     * @return {@link ParameterDefinition}
     * @see br.com.maps.util.report.metadata.ColumnMetaData#getParameterDefinition(java.lang.String)
     */
    public ParameterDefinition getParameterDefinition(final String localkey) {
        return this.parameters.get(localkey);
    }

    /**
     * @return {@link Collection}
     * @see br.com.maps.util.report.metadata.ColumnMetaData#getParameters()
     */
    public Collection<ParameterDefinition> getParameters() {
        return this.parameters.values();
    }

    /**
     * Gets the parameter type given its name.
     * 
     * @param parameterName the parameter name.
     * @return {@link ParameterType}
     */
    public ParameterType getParameterType(final String parameterName) {
        final ParameterDefinition def = this.parameters.get(parameterName);
        return def == null ? ParameterType.OBJECT : def.getType();
    }

    /**
     * @return int
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((this.key == null) ? 0 : this.key.hashCode());
        result = prime * result + ((this.label == null) ? 0 : this.label.hashCode());
        result = prime * result + ((this.parameters == null) ? 0 : this.parameters.hashCode());
        return result;
    }

    /**
     * @param name String
     * @return boolean
     */
    public boolean hasParameter(final String name) {
        return this.parameters.containsKey(name);
    }

    /**
     * @return String
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Name: " + this.label + " (" + this.key + ") with params: " + this.parameters.keySet();
    }

    /**
     * @return the exampleDataCell
     */
    public DataCell getExampleDataCell() {
        return this.exampleDataCell;
    }

    /**
     * @return the chartTypes
     */
    public EnumSet<ChartType> getChartTypes() {
        return EnumSet.copyOf(this.chartTypes);
    }
}
